import java.util.Collections;
import java.util.Date;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("即将进入网页访问系统");
        Website[] websites = new Website[50];
        int count = 0;
        while(true){
            websites[count] = new Website();
            System.out.println("目前的网站总访问量和平均访问量分别是:  "
            +Website.Count()+" , "+Website.AverageCount());
            System.out.println("以下是访问量排行榜");
            Collections.sort(Website.InintList);
            Collections.reverse(Website.InintList);
            for(Website w:Website.InintList){
                System.out.println(w);
            }
            System.out.println("请输入你要访问的网站的网址/(输入\"check\"以查看某网页的最后访问时间/\"clean\"清空访问信息):");
            String inputURL = sc.nextLine();
            if(inputURL.equals("check")){
                System.out.println("请输入要查询最后访问时间的网站的网址:");
                inputURL = sc.nextLine();
                for(Website w:Website.InintList) {
                    String equal = w.getURL();
                    String[] equals = equal.split("\\(");
                    if (equal.equals(inputURL) || equals[0].equals(inputURL)) {
                        System.out.println(w.getDate());
                    }
                }
            } else if (inputURL.equals("clean")){
                Website.clean();
            } else {
                websites[count].setURL(inputURL);
                websites[count].setCount(0);
                websites[count].setBody(null);
                int flag = 0;
                for (Website w : Website.InintList) {
                    String equal = w.getURL();
                    String[] equals = equal.split("\\(");
                    if (equal.equals(inputURL) || equals[0].equals(inputURL)) {
                        w.setDate(new Date());
                        System.out.println("以下是该网站的内容:\n" + w.getBody());
                        w.setCount(w.getCount() + 1);
                        flag = 1;
                        break;
                    }
                }
                if (flag == 0) {
                    websites[count].setDate(new Date());
                    System.out.println("该网址不存在...");
                    Website.InintList.add(websites[count]);
                    websites[count].setURL(inputURL + "(这是输入错误的网址)");
                    websites[count].setCount(websites[count].getCount() + 1);
                    count++;
                }
            }
        }

    }
}
